//
//  IGReportTableVC.m
//  jingdongfang
//
//  Created by BOE on 2017/9/13.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGReportVC.h"
#import "IGReportTypeApi.h"
#import "IGReportTopicApi.h"
#import "TPKeyboardAvoidingTableView.h"

@interface IGReportVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) IWTextView *moreTextField;
@property (nonatomic, copy) NSString *reportId;
@property (nonatomic, strong) TPKeyboardAvoidingTableView *tableView;
@property (nonatomic, strong) NSArray *reportType;

@end

@implementation IGReportVC

- (instancetype)initWithReportType:(NSArray *)reportType
{
    self = [super init];
    if (self) {
        _reportType = reportType;
        for (int i = 0; i < reportType.count; i++) {
            IGReportType *type = reportType[i];
            type.isSelected = YES;
            _reportId = type.typeId;
            break;
        }
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.title =@"举报";
    self.title = NSLocalizedString(@"Report", nil);
    
//    [self ig_setupRightButtonItemWithTitle:@"确定" image:nil highLightedImage:nil];
    [self ig_setupRightButtonItemWithTitle:NSLocalizedString(@"Confirm", nil) image:nil highLightedImage:nil];

    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}
    
- (IWTextView *)moreTextField {
    if (!_moreTextField) {
        _moreTextField = [[IWTextView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 200)];
//        _moreTextField.placeholder = @"多行输入";
        _moreTextField.placeholder = NSLocalizedString(@"Multiline Input", nil);

    }
    return _moreTextField;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
    
#pragma mark -
    
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
    
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"ReportTypeListCellID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    if (indexPath.section == 1) {
        [cell.contentView addSubview:self.moreTextField];
        
        return cell;
    }
    
    IGReportType *type = self.reportType[indexPath.row];
    cell.textLabel.font = [UIFont font14];
    cell.textLabel.textColor = [UIColor c4Color];
    cell.textLabel.text = type.typeName;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 0, 20, 20)];
    button.userInteractionEnabled = NO;
    [button setImage:[UIImage imageNamed:@"pushiGallery_Normal"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"pushiGallery_Selected"] forState:UIControlStateSelected];
    button.selected = type.isSelected;
    cell.accessoryView = button;
    
    return cell;
}
    
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 44.f;
    } else {
        return 200.f;
    }
}
    
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}
    
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return .01f;
}
    
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
//        return @"请选择举报类型";
        return NSLocalizedString(@"Please Select Reporting Type", nil);
    } else {
//        return @"举证";
        return NSLocalizedString(@"Evidence", nil);
    }
}
    
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.reportType.count;
    } else {
        return 1;
    }
}
    
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, [self tableView:tableView heightForHeaderInSection:section])];
    view.backgroundColor = [UIColor clearColor];
    NSString *title = [self tableView:tableView titleForHeaderInSection:section];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, view.bounds.size.width, view.bounds.size.height)];
    label.text = title;
    label.textColor = [UIColor blackColor];
    
    [view addSubview:label];
    
    return view;
}
    
    //点击确认提交
- (void)ig_rightButtonPressed:(id)sender {
    [self.view endEditing:YES];
    if (_reportId.length == 0) {
//        [self presentMessageTips:@"请选择举报类型"];
        [self presentMessageTips:NSLocalizedString(@"Please Select Reporting Type", nil)];

        return;
    }
    
    if (self.moreTextField.text.length == 0) {
//        [self presentMessageTips:@"请输入举报内容"];
        [self presentMessageTips:NSLocalizedString(@"Please Input Reporting Content", nil)];
        return;
    }
    
    __weak typeof(self) weakSelf= self;
    IGReportTopicApi *api = [[IGReportTopicApi alloc] initWithTypeId:_reportId pId:_pId content:self.moreTextField.text];
    [api startWithDecodeClass:[IGReportTopicResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject) {
            IGReportTopicResp *resp = responseObject;
            IGBaseRespHead *header = resp.resHeader;
            [weakSelf presentMessageTips:header.message];
//            [weakSelf presentMessageTips:@"提交成功"];
            [weakSelf presentMessageTips:NSLocalizedString(@"Report successfully!", nil)];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    }];
}
    
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    IGReportType *type = self.reportType[indexPath.row];
    _reportId = type.typeId;
    
    [self selButClick:indexPath];
}

- (void)selButClick:(NSIndexPath *)indexPath {
    for (IGReportType *type in self.reportType) {
        if ([type.typeId isEqualToString:_reportId]) {
            type.isSelected = YES;
        } else {
            type.isSelected = NO;
        }
    }

    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
    
@end
